﻿@model CalendarApp.Models.MonthDTO
@{ 
    Layout = null;
    var rand = new Random();
    System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
    DateTime firstDay = new DateTime(Model.DateToShow.Year, Model.DateToShow.Month, 1);
    DateTime prevMonth = Model.DateToShow.AddMonths(-1);
    DateTime nextMonth = Model.DateToShow.AddMonths(1);
    int daysFromPreviousMonth = 0;
    switch( firstDay.DayOfWeek){
        case DayOfWeek.Tuesday: daysFromPreviousMonth = 1; break;
        case DayOfWeek.Wednesday: daysFromPreviousMonth = 2; break;
        case DayOfWeek.Thursday: daysFromPreviousMonth = 3; break;
        case DayOfWeek.Friday: daysFromPreviousMonth = 4; break;
        case DayOfWeek.Saturday: daysFromPreviousMonth = 5; break;
        case DayOfWeek.Sunday: daysFromPreviousMonth = 6; break;
    }
    int daysInCurrentMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month);
    DateTime lastDay = new DateTime(@Model.DateToShow.Year, Model.DateToShow.Month, daysInCurrentMonth);
}

@Html.Partial("_CalendarMenu", Model.DateToShow)

<div id="CAL-Month-Table" data-bind=" template: { name: 'weekTemplate', foreach: weeks, as: 'week' }">
    <div class="CAL-Day-MonthView-Row CAL-MountView-DayHeaders">
    <div class="CAL-MonthView-DayNameHeader">
        Monday
    </div>
    <div class="CAL-MonthView-DayNameHeader">
        Thuesday
    </div>
    <div class="CAL-MonthView-DayNameHeader">Wednesday</div>
    <div class="CAL-MonthView-DayNameHeader">
        Thursday
    </div>
    <div class="CAL-MonthView-DayNameHeader">
        Friday
    </div>
    <div class="CAL-MonthView-DayNameHeader">
        Saturday
    </div>
    <div class="CAL-MonthView-DayNameHeader">
        Sunday
    </div>
</div>
    <script type="text/html" id="weekTemplate">
        <div class="CAL-Day-MonthView-Row" data-bind="template: { name: 'dayTemplate', foreach: Days, as: 'day' }"  ></div>
    </script>
    <script type="text/html" id="dayTemplate">
        <div class="CAL-Day-MonthView" data-bind="click: day_click, css: { 'CAL-Today': Date.format('YYYY MM DD') == moment().format('YYYY MM DD') }">
            <div><span data-bind="text: Date.format('D')" ></span></div>
            <!-- ko foreach: Entries -->
                <div class="CAL-Entry-MountView primary-5" data-bind="style: { backgroundColor: Category.Color }" ><span data-bind="text:DisplayTitle, title:Description" ></span></div>
            <!-- /ko -->
        </div>
    </script>
</div>


<div id="CAL-New-Entry-Dialog" class="CAL-Dialog">
    <div  data-bind="with: current_entry"  class="div-table " >
        <div class="div-table-row">
            <div class="div-table-col CAL-EntryDialog-Col1">When</div>
            <div class="div-table-col CAL-EntryDialog-Col2"><span data-bind="text: StartDate().format('LL')"></span></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col CAL-EntryDialog-Col1">What</div>
            <div class="div-table-col CAL-EntryDialog-Col2"><input id="CAL-NewEntry-Title" type="text" data-bind="value: Title, valueUpdate: 'afterkeydown'" /></div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col CAL-EntryDialog-Col1 vertical-align-top">Description</div>
            <div class="div-table-col CAL-EntryDialog-Col2" >
                <textarea data-bind="value: Description"></textarea>
            </div>
        </div>
        <div class="div-table-row">
            <div class="div-table-col CAL-EntryDialog-Col1">Category</div>
            <div class="div-table-col CAL-EntryDialog-Col2">

                <select data-bind="options: $parent.categories, 
                   optionsText: function(item) { 
                       return item.Name; 
                   }, 
                   value: Category">
                </select>
            </div>
        </div>
    </div>
</div>


@*@{var renderedDayCount = 0;}
@for (int week = 0; week < 5; week++)
{
    <div class="CAL-Day-MonthView-Row">

        @if(week == 0){
            for (int i = 1; i <= daysFromPreviosMonth; i++)
            {
                renderedDayCount++;
                var day = @DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month) - daysFromPreviosMonth + i;
                var date = new DateTime(prevMonth.Year, prevMonth.Month, day);
                <div class="CAL-Day-MonthView CAL-NotCurrentMountDay" data-bind="click: day_click" data-date="@date.ToString("yyyy-MM-dd")" title="@date.ToString("MMMM dd. (dddd)",ci)" >
                    <div><span>@day</span></div>
                    @if(rand.Next(0,2)==1){
                    <div class="CAL-Entry-MountView primary-5" >16:00 Tesztadat</div>}
                    @if(rand.Next(0,2)==1){
                    <div class="CAL-Entry-MountView secondary-a-5" >18:00 Tesztadat kifér</div>}
                    @if(rand.Next(0,2)==1){
                    <div class="CAL-Entry-MountView secondary-b-5" >20:00 Valami más</div>}
                </div>
            }
            for (var i = 1; i <= 7 - daysFromPreviosMonth; i++)
            {
            renderedDayCount++;
            var date = new DateTime(Model.DateToShow.Year, Model.DateToShow.Month, i);
            <div class="CAL-Day-MonthView @(date == DateTime.Today ? "CAL-Today" : "")" data-bind="click: day_click" data-date="@date.ToString("yyyy-MM-dd")" title="@date.ToString("MMMM dd. (dddd)",ci)">
                <div><span>@i</span><span class="CAL-MonthView-Day-DayName"> @date.ToString("dddd", ci)</span></div>
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView primary-5" >16:00 Tesztadat</div>}
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView secondary-a-5" >18:00 Tesztadat kifér</div>}
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView secondary-b-5" >20:00 Valami más</div>}
            </div>
            }
        }
        
        @{var weekday = 7-daysFromPreviosMonth + 1;}
        @do
        {
            renderedDayCount++;
            var day = week * 7 + weekday;
            if (day > daysInCurrentMonth) { break; }
            var date = new DateTime(Model.DateToShow.Year, Model.DateToShow.Month, day);
            <div class="CAL-Day-MonthView @(date == DateTime.Today ? "CAL-Today" : "")" data-bind="click: day_click" data-date="@date.ToString("yyyy-MM-dd")" title="@date.ToString("MMMM dd. (dddd)",ci)" >
                <div><span>@day</span><span class="CAL-MonthView-Day-DayName"> @date.ToString("dddd", ci)</span></div>
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView primary-5" >16:00 Tesztadat</div>}
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView secondary-a-5" >18:00 Tesztadat kifér</div>}
                @if (rand.Next(0, 2) == 1)
                {
                <div class="CAL-Entry-MountView secondary-b-5" >20:00 Valami más</div>}
                <div data-bind="foreach: getDayEntries('@date.ToString("yyyy-MM-dd")')">
                    <div class="CAL-Entry-MountView secondary-b-5" > <span> ---- - -- -</span> </div>
                </div>
            </div>
                weekday++;
        } while (renderedDayCount % 7 != 0);
    </div>
}*@
